package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.bean.ItemInformation;
import com.wolfalpha.utron.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * （资讯）列表适配器
 * <p/>
 * Created by luodon on 15/8/19.
 */
public class ItemInformationSimpleAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context;

    private List<ItemInformation> informations = new ArrayList<ItemInformation>();

    public ItemInformationSimpleAdapter(Context context, List<ItemInformation> rs) {
        inflater = LayoutInflater.from(context);
        if (rs != null) {
            informations = rs;
        }
        this.context = context;

    }

    /**
     * 改变数据
     *
     * @param rs
     */
    public void setData(List<ItemInformation> rs) {
        if (rs != null) {
            informations = rs;
        }

        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return informations.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_information_simple, null);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.item_tv_information_simple_title);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.item_tv_information_simple_time);
            viewHolder.ivPic = (ImageView) convertView.findViewById(R.id.item_iv_information_simple_head);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置控件内容
        viewHolder.tvTitle.setText(informations.get(position).getTitle());
        viewHolder.tvTime.setText(String.valueOf(StringUtils.friendly_time(StringUtils.TimeStamp2Date(informations.get(position).getTime() * 1000))));
        //设置图片
        Picasso.with(context)
                .load(informations.get(position).getFace())
                .placeholder(R.mipmap.project_default)
                .into(viewHolder.ivPic);
        return convertView;
    }


    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvTitle;       //标题
        private TextView tvTime;        //发布时间
        private ImageView ivPic;        //介绍图
    }
}
